<?php

/**
 * 前台内容模型
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: ContentModel.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace App\Model;
class ContentModel extends CommonModel{
    public $addTableRow;
    protected $_link = array(
        'Contype' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Contype',
            'foreign_key' => 'typeid',
            'as_fields' => 'typedir,typename,isdefault,defaultname,namerule,namerule2,ispart',
        ),
        'Model' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Model',
            'foreign_key' => 'mid',
            'as_fields' => 'addtable',
        )
    );
    /**
     * 得到列表数据
     * @param  array $where   查询条件
     * @param  string $fields 插叙字段
     * @return array
     */
    public function getPagination($where, $fields, $order, $firstRow, $listRows) {
        $result = parent::getPagination($where, $fields, $order, $firstRow, $listRows);
        
        if($this->TypeInfos['addtable'] != ""){
            $M = M($this->TypeInfos['addtable']);
            foreach($result as $key => $val){
                //获取附表数据
                $addtableData = $M->getByAid($val['id']);
                //合并附表数据
                $result[$key] = array_merge($result[$key], $addtableData);
                //判断是否有缩略图
                $result[$key]['litpic'] = isLitpic($result[$key]['litpic']);
                //生成文档链接
                $result[$key]['arcurl'] = GetFileUrl($result[$key]['id'],$result[$key]['typeid'],$result[$key]['senddate'],$result[$key]['title'],$result[$key]['ismake'],
                    $result[$key]['namerule'],$result[$key]['typedir'],$result[$key]['filename']);
                 //生成栏目链接
                $result[$key]['typeurl'] = GetTypeUrl($result[$key]['typeid'],MfTypedir($result[$key]['typedir']),$result[$key]['isdefault'],$result[$key]['defaultname'], $result[$key]['ispart'],$result[$key]['namerule2']);
            }
        }
        return $result;
    }
    
    /**
     * 获取文档详细
     * @param int $aid      文档ID
     * @param bool|string $relation     是否关联查询
     * @return array
     */
    public function getContent($aid, $relation = "Model") {
        $result = $this->relation($relation)->getById($aid);
        if($result['addtable'] != ""){
            $this->addTableRow = D($result['addtable'])->getByAid($result['id']);
            if(isArrayAndNotEmpty($this->addTableRow))
            {
                $result['redirecturl'] = $this->addTableRow['redirecturl'];
                $result['templet'] = $this->addTableRow['templet'];
                $result['userip'] = $this->addTableRow['userip'];
            }
            $result['templet'] = (empty($result['templet']) ? '' : trim($result['templet']));
            $result['redirecturl'] = (empty($result['redirecturl']) ? '' : trim($result['redirecturl']));
            $result['userip'] = (empty($result['userip']) ? '' : trim($result['userip']));
        $result = array_merge($result, $this->addTableRow);
        }else{
            $result['templet'] = $result['redirecturl'] = '';
        }
        return $result;
    }
    
    public function getOneArc($where) {
        $result = $this->where($where)->find();
    }
    /**
     * 获取文档
     * @param string|array $map     查询条件
     * @param string $fields  查询自动
     * @param string|array $join    关联查询
     * @param string|int $limit   limit
     * @param string $orderby 排序
     * @return array
     */
    public function getArcList($map, $fields, $limit, $orderby = 'c.id desc', $join = '') {
        $result = $this->cache(true, 3600)->alias("c")->where($map)->field($fields)->join($join)->order($orderby)->limit($limit)->select();
        //echo $this->getLastSql();
        //var_dump(S('arclist', NULL));
        return isArrayAndNotEmpty($result) ? $result : NULL;
    }
}
